
#include <gtk/gtk.h>

#include "account.h"
#include "chatapp.h"
#include "serverset.h"

struct _ServerSet
{
  GtkDialog parent;
};

typedef struct _ServerSetPrivate ServerSetPrivate;

struct _ServerSetPrivate
{
  GSettings *settings;
  GtkWidget *font;
  GtkWidget *transition;
  GtkWidget *network_entry;
  GtkWidget *id_entry;
  GtkWidget *pwd_entry;
  GtkWidget *sign_in;
  GtkWidget *notice;

  gpointer conn;
  gchar *network, *id, *pwd;
};

G_DEFINE_TYPE_WITH_PRIVATE(ServerSet, server_set, GTK_TYPE_DIALOG)

extern gpointer read_thread(gpointer user_data);
extern void *sign_in(void *conn, void *user_data);

void socket_client_event (GSocketClient      *client,
               GSocketClientEvent  event,
               GSocketConnectable *connectable,
               GIOStream          *connection,
               gpointer            user_data)
{
	static GEnumClass *event_class;
	gint64 now_us;

	if (!event_class)
		event_class = g_type_class_ref (G_TYPE_SOCKET_CLIENT_EVENT);

	now_us = g_get_real_time ();
	g_print ("%" G_GINT64_FORMAT " GSocketClient => %s [%s]\n",
		now_us,
		g_enum_get_value (event_class, event)->value_nick,
		connection ? G_OBJECT_TYPE_NAME (connection) : "");
	if (event == G_SOCKET_CLIENT_CONNECTED) {
		g_thread_new("client", read_thread, user_data);
    g_object_set_data(user_data, "conn", connection);
    sign_in(connection, user_data);
	//} else if (event == G_SOCKET_CLIENT_DISCONNECTED) {
  }
}
void *server_connect (const char *network, gpointer user_data)
{
	GError *error = NULL;
	GSocketClient *client;
	GSocketConnection *conn;

	client = g_socket_client_new ();
	g_signal_connect (client, "event", G_CALLBACK (socket_client_event),
			user_data);
	conn = g_socket_client_connect_to_host(client, network, CHAT_DEFAULT_PORT,
			NULL, &error);
	/*
//	g_socket_client_set_timeout(client, 2);
	addr_s = g_network_address_new (ip, port);
	conn = g_socket_client_connect(client, addr_s, NULL, &error);
	*/
	if (error) {
		g_debug("%s%s\n", error->message, network);
		goto err;
	}
err:
	return conn;
}
static void sign_in_cb (GtkButton *button, gpointer user_data)
{
	const char *network = NULL;
  int status = 0;
	ServerSet *win;
	ServerSetPrivate *priv;
  gpointer *cwin = NULL, *conn = NULL;
  GSettings *settings = NULL;

	win = user_data;
	priv = server_set_get_instance_private (win);
  settings = priv->settings;
 	gtk_button_set_label(button, "logging in");
	network = gtk_entry_get_text(GTK_ENTRY(priv->network_entry));
  g_object_get(win, "transient-for", &cwin, NULL);
  g_print("%s[%d]cwin:%p\n", __FILE__, __LINE__, cwin);
  conn = server_connect(network, cwin);
  if (conn) {
    sleep(1);
    status = g_settings_get_int(settings, "status");
		gtk_label_set_text(GTK_LABEL(priv->notice), account_status_to_str(status));
    gtk_button_set_label(GTK_BUTTON(priv->sign_in),
        account_status_to_button(status));
	}
}
/*
static void
status_change_cb (GObject    *gobject,
               GParamSpec *pspec,
               gpointer    user_data)
{
  ServerSet *win;
	ServerSetPrivate *priv;
  int status = 0;

  win = user_data;
	priv = server_set_get_instance_private (win);
 // g_object_get(gobject, "status", &status, NULL);
  g_debug("status:%d\n", status);
  if (status == ACCOUNT_STATUS_DISCONNECTED) {
		//gtk_label_set_text(GTK_LABEL(priv->notice), account_status_to_str(status));
		gtk_label_set_text(GTK_LABEL(priv->notice), "fs");
  }
}
*/
static void
print_hello_cb()
{
  g_debug("8888888888888888888888888fds\n");
}
static void
server_set_init (ServerSet *win)
{
	ServerSetPrivate *priv;
  GSettings *settings;
	char *obj_str = NULL;
  int status = 0;

	priv = server_set_get_instance_private (win);
	gtk_widget_init_template (GTK_WIDGET (win));
	priv->settings = g_settings_new ("org.gtk.app");
  settings = priv->settings;

	obj_str = g_settings_get_string(priv->settings, "network");
	gtk_entry_set_text(GTK_ENTRY(priv->network_entry), obj_str);
	obj_str = g_settings_get_string(priv->settings, "id");
	gtk_entry_set_text(GTK_ENTRY(priv->id_entry), obj_str);
	obj_str = g_settings_get_string(priv->settings, "pwd");
	gtk_entry_set_text(GTK_ENTRY(priv->pwd_entry), obj_str);
	g_signal_connect (priv->sign_in, "clicked",  G_CALLBACK (sign_in_cb), win);
	g_signal_connect (priv->settings, "notify::status",  G_CALLBACK (print_hello_cb),
      win);
  g_settings_bind (priv->settings, "network", priv->network_entry, "text",
                   G_SETTINGS_BIND_DEFAULT);
  g_settings_bind (priv->settings, "id", priv->id_entry, "text",
                   G_SETTINGS_BIND_DEFAULT);
  g_settings_bind (priv->settings, "pwd", priv->pwd_entry, "text",
                   G_SETTINGS_BIND_DEFAULT);
  g_settings_bind (priv->settings, "notice", priv->notice, "label",
                   G_SETTINGS_BIND_DEFAULT);
  status = g_settings_get_int(settings, "status");
  gtk_label_set_text(GTK_LABEL(priv->notice), account_status_to_str(status));
  gtk_button_set_label(GTK_BUTTON(priv->sign_in), account_status_to_button(status));
}

static void
server_set_dispose (GObject *object)
{
  ServerSetPrivate *priv;

  priv = server_set_get_instance_private (SERVER_SET (object));
  g_clear_object (&priv->settings);

  G_OBJECT_CLASS (server_set_parent_class)->dispose (object);
}

static void
server_set_class_init (ServerSetClass *class)
{
  G_OBJECT_CLASS (class)->dispose = server_set_dispose;

  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
                                               "/org/gtk/app/serverset.ui");
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			ServerSet, network_entry);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			ServerSet, id_entry);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			ServerSet, pwd_entry);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			ServerSet, sign_in);
	gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class),
			ServerSet, notice);
}

ServerSet *
server_set_new (ServerSet *win)
{
  return g_object_new (SERVER_SET_TYPE, "transient-for", win,
      "use-header-bar", TRUE, NULL);
}
